package tp.pr5;

import jargs.gnu.CmdLineParser;

import java.io.*;

import tp.pr5.Game;
import tp.pr5.console.GameControllerConsole;
import tp.pr5.gui.GameControllerGUI;
import tp.pr5.gui.MainWindow;
import tp.pr5.maploader.MapLoaderFromTxtFile;
import tp.pr5.maploader.exceptions.WrongMapFormatException;

public class Main
{

	private static Map loadFile(String nombre) throws IOException
	{
		Map mapa = null;
		FileInputStream file = null;
		MapLoaderFromTxtFile mlftf = new MapLoaderFromTxtFile();
		file = new FileInputStream(nombre);
		mapa = mlftf.loadMap(file);
		file.close();
		if (mapa == null)
			throw new WrongMapFormatException("");
		return mapa;

	}

	public static void main(java.lang.String[] args)
	{

		CmdLineParser parsear = new CmdLineParser();
		CmdLineParser.Option interfaz = parsear.addStringOption('i', "interface");
		CmdLineParser.Option fileName = parsear.addStringOption('m', "map");
		try
		{
			parsear.parse(args);
		} catch (CmdLineParser.OptionException e)
		{
			System.err.println(e.getMessage());
		}

		String interfazType = (String) parsear.getOptionValue(interfaz);
		String mapFile = (String) parsear.getOptionValue(fileName);
		// interfazType="console";
		// mapFile="map.txt";
		if (interfazType.equalsIgnoreCase("console"))
		{

			try
			{
				Map mapa = loadFile(mapFile);

				Game g = new Game(mapa);
				GameControllerConsole gC=new GameControllerConsole(g);
				gC.runGame();
			} catch (ArrayIndexOutOfBoundsException e1)
			{
				System.err.println("No map file specified.\n\nUsage: tp.pr5.Main <mapFile>\n");
				System.exit(1);
			} catch (IOException e)
			{
				System.err.println("File not valid\n");
				// System.out.println(e.getCause().getMessage());
				System.exit(1);
			}
			// System.exit(0);

		}
		else if (interfazType.equalsIgnoreCase("swing"))
		{
			try
			{
				Map mapa = loadFile(mapFile);

				Game g = new Game(mapa);
				GameControllerGUI gC=new GameControllerGUI(g);
				MainWindow m = new MainWindow(gC);
				m.setVisible(true);
				g.requestStart();

			} catch (ArrayIndexOutOfBoundsException e1)
			{
				System.err.println("No map file specified.\n\nUsage: tp.pr5.Main <mapFile>\n");
				System.exit(1);
			} catch (IOException e)
			{
				System.err.println("File not valid\n");
				// System.out.println(e.getCause().getMessage());
				System.exit(1);
			}
			// System.exit(0);
		}
		else if (interfazType.equalsIgnoreCase("both"))
		{
			try
			{
				Map mapa = loadFile(mapFile);

				Game g = new Game(mapa);
				GameControllerGUI gC=new GameControllerGUI(g);
				GameControllerConsole gCC=new GameControllerConsole(g);
				
				MainWindow m = new MainWindow(gC);
				//gCC.runGame();
				m.setVisible(true);
				gC.runBoth();

			} catch (ArrayIndexOutOfBoundsException e1)
			{
				System.err.println("No map file specified.\n\nUsage: tp.pr5.Main <mapFile>\n");
				System.exit(1);
			} catch (IOException e)
			{
				System.err.println("File not valid\n");
				// System.out.println(e.getCause().getMessage());
				System.exit(1);
			}
			// System.exit(0);
		}
		else System.exit(1);
	}

}
